package com.bdjk.bdmw.InfoGath.communication.server.tcp.socket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import com.bdjk.bdmw.InfoGath.common.constants.InfoGathConstants;

/**
 * @author hy
 *
 */
public class MainServer implements Runnable{
	private static ServerSocket serverSocket;
	
	@Override
	public void run() {
		try {
			serverSocket = new ServerSocket(Integer.valueOf(InfoGathConstants.TCP_PORT));
			while (true) {
				// 一直处于监听状态,这样可以处理多个用户
				Socket socket = serverSocket.accept();
				
				// 启动读写线程
				new Thread(new CommunicationThread(socket)).start();
				//new ServerOutputThread(socket).start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e){
			
		} finally {
			try {
				serverSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void shutDown(){
		try {
			serverSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws Exception {
		new Thread(new MainServer()).start();
	}
}
